package com.duobaoyu.dby.supply.biz.strategy.message.delay;

import com.alibaba.fastjson.JSON;
import com.duobaoyu.dby.channel.adapter.enums.BizScenarioEnum;
import com.duobaoyu.dby.channel.adapter.enums.biz.refund.AfterSaleStatusEnum;
import com.duobaoyu.dby.common.mybatis.util.EnumUtils;
import com.duobaoyu.dby.supply.biz.constant.OperateSourceConstants;
import com.duobaoyu.dby.supply.biz.service.queue.core.DistributionQueueMessage;
import com.duobaoyu.dby.supply.biz.service.queue.service.AbstractDistributionQueueJobConsumer;
import com.duobaoyu.dby.supply.biz.service.refund.AfterSaleService;
import com.duobaoyu.dby.supply.biz.service.refund.AfterSaleStateMachine;
import com.duobaoyu.dby.supply.biz.service.refund.event.AfterSaleCreateEvent;
import com.duobaoyu.dby.supply.common.biz.bo.refund.AfterSaleOutBO;
import com.duobaoyu.dby.supply.common.biz.bo.refund.AfterSaleQuery;
import com.duobaoyu.dby.supply.common.biz.bo.refund.AfterSaleStateContext;
import com.duobaoyu.dby.supply.core.dinger.annotation.DingerTalk;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by shuming on 2023/10/23 11:36
 * 延迟提交售后
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class DelaySubmitChannelAfterSaleDistributionQueueJobConsumer extends AbstractDistributionQueueJobConsumer {

    @Value("${delay.submitChannelAfterSale.topicName}")
    private String delaySubmitChannelAfterSaleTopicName;

	private final AfterSaleStateMachine afterSaleStateMachine;
	private final AfterSaleService afterSaleService;

    @Override
    public String topicName() {
        return delaySubmitChannelAfterSaleTopicName;
    }

    @Override
    @DingerTalk(title = "延迟提交售后消息")
    public void consume(DistributionQueueMessage delayMessage) {
        if (EnumUtils.eq(BizScenarioEnum.DELAY_SUBMIT_CHANNEL_AFTER_SALE,delayMessage.getBizScenario())) {
            String data = delayMessage.getData();
            AfterSaleCreateEvent event = JSON.parseObject(data, AfterSaleCreateEvent.class);
			AfterSaleQuery query = new AfterSaleQuery();
			query.setAfterSaleSn(event.getAfterSaleSn());
			AfterSaleOutBO afterSaleOutBO = afterSaleService.queryAfterSale(query);
			// 发送状态变更事件
			AfterSaleStateContext afterSaleStateContext = new AfterSaleStateContext();
			afterSaleStateContext.setAfterSale(afterSaleOutBO);
			afterSaleStateContext.setOperateSource(OperateSourceConstants.BACKSTAGE);
			afterSaleStateContext.setCurrentAfterSaleStatus(AfterSaleStatusEnum.TO_BE_SUBMITTED_FOR_CHANNEL.getValue());
			afterSaleStateContext.setChangeContentOperate(AfterSaleStateContext.ChangeContentOperate.builder().build());
			afterSaleStateMachine.exe(afterSaleStateContext);
        }
    }
}
